import os,django
os.environ.setdefault('DJANGO_SETTINGS_MODULE','wxy_django.settings')
django.setup()
#指定django的配置

from  user import  models

print(models.Category.objects.all())#查询所有的数据

obj=models.Category.objects.get(id=1)#只返回一条数据，返回多条数据会报错

# 查询数据里面的字段
print(obj.name)
print(obj.create_time)
print(obj.update_time)

#基本查询
print(models.Article.objects.filter(category_id=2))
print(models.Article.objects.get(category_id=2))

#新增第一种
models.Article.objects.create(
    title="python123",
    desc="这是描述",
    content="文章内容",
    category_id=2,
    img="https://image.baidu.com/search/detail?ct=503316480&z=0&ipn=d&word=%E5%9B%BE%E7%89%87&step_word=&hs=0&pn=7&spn=0&di=1240&pi=0&rn=1&tn=baiduimagedetail&is=0%2C0&istype=0&ie=utf-8&oe=utf-8&in=&cl=2&lm=-1&st=undefined&cs=233301930%2C3031623456&os=4002187338%2C11148053&simid=0%2C0&adpicid=0&lpn=0&ln=1830&fr=&fmq=1621156603845_R&fm=&ic=undefined&s=undefined&hd=undefined&latest=undefined&copyright=undefined&se=&sme=&tab=0&width=undefined&height=undefined&face=undefined&ist=&jit=&cg=&bdtype=11&oriquery=&objurl=https%3A%2F%2Fgimg2.baidu.com%2Fimage_search%2Fsrc%3Dhttp%3A%2F%2Fup.enterdesk.com%2Fedpic%2F4c%2Fa6%2F31%2F4ca631a8841304be2351295d50cf801d.jpg%26refer%3Dhttp%3A%2F%2Fup.enterdesk.com%26app%3D2002%26size%3Df9999%2C10000%26q%3Da80%26n%3D0%26g%3D0n%26fmt%3Djpeg%3Fsec%3D1623748607%26t%3D246a17efd499720f13e0273e3e47101a&fromurl=ippr_z2C%24qAzdH3FAzdH3Fooo_z%26e3Bjgpj61jfh_z%26e3Bv54AzdH3FktzitAzdH3Fnl08c_z%26e3Bip4s&gsm=4&rpstart=0&rpnum=0&islist=&querylist=&force=undefined"
)

#新增第二种
art=models.Article(title="python456",desc="miaoshu",content="pythong456456",category_id=2,img="https://image.baidu.com/search/detail?ct=503316480&z=0&ipn=d&word=%E5%9B%BE%E7%89%87&step_word=&hs=0&pn=7&spn=0&di=1240&pi=0&rn=1&tn=baiduimagedetail&is=0%2C0&istype=0&ie=utf-8&oe=utf-8&in=&cl=2&lm=-1&st=undefined&cs=233301930%2C3031623456&os=4002187338%2C11148053&simid=0%2C0&adpicid=0&lpn=0&ln=1830&fr=&fmq=1621156603845_R&fm=&ic=undefined&s=undefined&hd=undefined&latest=undefined&copyright=undefined&se=&sme=&tab=0&width=undefined&height=undefined&face=undefined&ist=&jit=&cg=&bdtype=11&oriquery=&objurl=https%3A%2F%2Fgimg2.baidu.com%2Fimage_search%2Fsrc%3Dhttp%3A%2F%2Fup.enterdesk.com%2Fedpic%2F4c%2Fa6%2F31%2F4ca631a8841304be2351295d50cf801d.jpg%26refer%3Dhttp%3A%2F%2Fup.enterdesk.com%26app%3D2002%26size%3Df9999%2C10000%26q%3Da80%26n%3D0%26g%3D0n%26fmt%3Djpeg%3Fsec%3D1623748607%26t%3D246a17efd499720f13e0273e3e47101a&fromurl=ippr_z2C%24qAzdH3FAzdH3Fooo_z%26e3Bjgpj61jfh_z%26e3Bv54AzdH3FktzitAzdH3Fnl08c_z%26e3Bip4s&gsm=4&rpstart=0&rpnum=0&islist=&querylist=&force=undefined")
art.save()

#批量按条件修改
models.Article.objects.filter(show_count=0).update(show_count=500)

#批量修改全部
models.Article.objects.all().update(show_count=900)

#单个删除
a=models.Article.objects.get(id=5)
a.delete()

#按条件删除
models.Article.objects.filter(show_count=0).delete()#按照条件删除

#删除所有的
models.Article.objects.all().delect()



#查询多条件的
from user import models
f=models.Article.objects.filter(id__gt=1) #大于
f=models.Article.objects.filter(id__lt=5) #小于
f=models.Article.objects.filter(id__gte=5) #大于等于
f=models.Article.objects.filter(id__lte=5) #小于等于

f=models.Article.objects.filter(title__contains="静")#内容包含
f=models.Article.objects.filter(title__startswith='P')#开头包含，区分大小写查询开头包含字母
f=models.Article.objects.filter(title__istartswith='P')#不区分大小写查询开头包含字母
f=models.Article.objects.filter(title__endswith='思')#结尾包含
f=models.Article.objects.filter(img="")#查询一个字段为空
f=models.Article.objects.filter(img="",title__startswith="静")#and条件
from django.db.models import Q
f=models.Article.objects.filter(Q(tag__isnull=True)|Q(img=""))#或条件,需要导入Q包
f=models.Article.objects.filter(id__in=[1,2,3])#id在1，2，3
f=models.Article.objects.exclude(id__in=[1,2,3])#id不在1，2，3
f=models.Article.objects.exclude(img="")#img不等于空
f=models.Article.objects.exclude(img="").count()#出现多少次
f=models.Article.objects.all().order_by("create_time")#默认升序，可以根据多个字段排序
f=models.Article.objects.all().order_by("-create_time")#降序
f=models.Article.objects.get(id=10)#如果不存在会报错
f=models.Article.objects.filter(id=2).first()#返回数据列表的第一条，可以通过f.title方式取到字段值
f=models.Article.objects.filter(id=2).last()#返回数据列表的最后一条
f=models.Article.objects.all()[:10]#limit，返回前10条
f=models.Article.objects.filter(img="").values("title").distinct()#去重,values可以写多个字段
f=models.Category.objects.all().values()
print(f)
f=models.Article.objects.filter(id=2).exists()

#1对多的查询，通过1查询多，1.多_set.all()
f=models.Category.objects.get(name="python")
print(f.article_set.all()) #查询python下面的所有文章
print(f.article_set.count())#查询python下面的数量
print(f.article_set.filter(show_count__gt=500))#查询python大于500个浏览量的文章

#1对多的时候，通过多查询1
f=models.Article.objects.get(id=1)
print(f.category.name)#通过某个文章查询文章的所属分类
print(f.category.article_set.all())#通过文章查询文章的分类，再查询分类下面的所有文章

#分页
from django.core.paginator import Paginator
L=range(100)
p=Paginator(L,10)#第一个参数是你要分页的数据，是一个list，第二个参数是每页的数据
print(list(p.get_page(2)))
print(p.num_pages)#总页数
print(p.count)#总数
page=p.page(1)
print(page.has_previous())#是否有上一页
print(page.has_next())#是否有下一页
print(page.has_other_pages())#是否有其他页
print(page.next_page_number())#下一个页的页码
print(page.previous_page_number())#上一页的页码
print(p.page_range)#获取分页的范围
print(page.number)#获取当前页码